<?php
namespace app\admin\controller\staff;;

use app\admin\model\EmployeeInformation;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use app\admin\model\publicuse\PublicUse;
use smartybuilder\libs\Quickbuilder;
use think\App;
/**
 * @ControllerAnnotation(title="员工管理")
 * Class People
 */
class People extends AdminController
{
    use \app\admin\traits\Curd;
     protected $allowModifyFields = [
        'status',
    ];
    protected $noExportFields = [
        'id',
        'name',
        'sort',
        'phone',
        'pwd',
        'head_img',
        'create_time',
        'update_time',
        'status',
        'delete_time',
    ];
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new \app\admin\model\StaffPeople();
        $array = PublicUse::getConfigDir(__CLASS__);
        if(is_file(__DIR__.DIRECTORY_SEPARATOR.'field'.DIRECTORY_SEPARATOR.$array['class'].'.txt')){
           $this->AllData = json_decode(file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'field'.DIRECTORY_SEPARATOR.$array['class'].'.txt'),true);
        }
    }
    public function add()
    {
        $data = request()->param();
        if ($this->request->isAjax()) {
            $post = $this->request->post();
//            return $this->error('不从',$this->AllData);
            $rule = [];
            $this->validate($post, $rule);
//            return $this->error('保存失败2',$post);
            if(isset($this->AllData['field'])&&!empty($this->AllData['field'])){
                $post = PublicUse::Conversion($post,$this->AllData['field']);
            }
            if(isset($post['pwd'])&&$post['pwd']){
                $post['pwd'] = password($post['pwd']);
            }
//            return $this->error('保存失败1',$post);
            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:'.$e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        if(isset($this->AllData['field'])){
            if($this->AllData['field']){
                $this->AllData['field'] = PublicUse::FieldGetLianJie($this->AllData['field']);
            }
//            echo '<pre>';
//            print_r($this->AllData['field']);
//            exit;
            if($this->AllData['tabletype']=='tree'){
                if(isset($this->AllData['field']['pid']['option'])&&$this->AllData['field']['pid']['option']){
                    $this->AllData['field']['pid']['option'] = PublicUse::getPidMenuList($this->AllData['field']['pid']['option']);
                }
            }
            $this->assign('alldata',$this->AllData);
        }
        if(isset($data['field'])&&isset($data['id'])&&$data['field']&&$data['id']){
            $data[$data['field']] = $data['id'];
            unset($data['field']);
            unset($data['id']);
        }
        $this->assign('row',$data);
        return $this->fetch();
    }
    public function edit()
    {
        $id = request()->param($this->model->getPk());
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
//            return $this->error('保存失败',$post);
            if(isset($this->AllData['field'])&&!empty($this->AllData['field'])){
                $post = PublicUse::Conversion($post,$this->AllData['field']);
            }
            if(isset($post['pwd'])&&$post['pwd']){
                $post['pwd'] = password($post['pwd']);
            }else{
                if(isset($post['pwd'])){
                    unset($post['pwd']);
                }
            }
//            return $this->error('保存失败',$post);
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $row->pwd = '';
        $this->assign('row', $row);
        if(isset($this->AllData['field'])){
            if($this->AllData['field']){
                $this->AllData['field'] = PublicUse::FieldGetLianJie($this->AllData['field'],$row);
            }
            if($this->AllData['tabletype']=='tree'){
                if(isset($this->AllData['field']['pid']['option'])&&$this->AllData['field']['pid']['option']){
                    $this->AllData['field']['pid']['option'] = PublicUse::getPidMenuList($this->AllData['field']['pid']['option']);
                }
            }
//            echo '<pre>';
//            print_r($this->AllData['field']['my_lselect']);
//            exit;
            $this->assign('alldata',$this->AllData);
        }
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="重置密码")
     */
    public function chognzhi(){
        $id = $this->request->param('id');
        $data = $this->request->param();
        if(isset($data['pwd'])){
            if(empty($data['pwd'])){
                return $this->error('请输入密码');
            }
            $shuju = $this->model->find($id);
            $shuju->pwd = password($data['pwd']);
            $shuju->update_time = time();
            $bool = $shuju->save();
            if(!$bool){
                return $this->error('修改失败');
            }
            return $this->success('修改成功');
        }else{
            return Quickbuilder::makeForm()
                ->setAction()
                ->set_up_form('none','','id',$id)
                ->set_up_form('input','密码','pwd','')
                ->set_button('submit','提交')
                ->fetch();
        }
    }
    /**
     * @NodeAnotation(title="资料")
     */
    public function ziliao(){
        $id = $this->request->param('id');
        $shuju = EmployeeInformation::where('staff_people_id',$id)
            ->find();
        if(empty($shuju)){
            $url = '/admin/staff.information/add?staff_people_id='.$id;
        }else{
            $url = '/admin/staff.information/edit?id='.$shuju->id;
        }
        return $this->redirect($url);
    }
}